/**
 * 
 */
package br.com.lazyprogrammer.worldsoccermanager.network.model.dao;

import java.util.Date;

import android.content.Context;
import android.os.AsyncTask;

import br.com.lazyprogrammer.worldsoccermanager.R;
import br.com.lazyprogrammer.worldsoccermanager.network.Network;
import br.com.lazyprogrammer.worldsoccermanager.network.Request;
import br.com.lazyprogrammer.worldsoccermanager.network.interfaces.LoginListener;
import br.com.lazyprogrammer.worldsoccermanager.network.interfaces.RegisterListener;
import br.com.lazyprogrammer.worldsoccermanager.network.task.IsEmailAvailableTask;
import br.com.lazyprogrammer.worldsoccermanager.network.task.IsUsernameAvailableTask;
import br.com.lazyprogrammer.worldsoccermanager.network.task.LoginTask;
import br.com.lazyprogrammer.worldsoccermanager.network.task.OfflineTask;
import br.com.lazyprogrammer.worldsoccermanager.network.task.OnlineTask;
import br.com.lazyprogrammer.worldsoccermanager.network.task.RegisterTask;
import br.com.lazyprogrammer.worldsoccermanager.network.task.ValidateTask;

/**
 * 
 * @author Limao Games <br />
 * <b>Email:</b> andreikeda@limaogames.com.br <br />
 * created on: 30/04/2014
 * @version 1.0
 * @see 
 */
public class UserDAO {
	
	private IsEmailAvailableTask isEmailAvailableTask;
	private IsUsernameAvailableTask isUsernameAvailableTask;
	private LoginTask loginTask;
	private OfflineTask offlineTask;
	private OnlineTask onlineTask;
	private RegisterTask registerTask;
	private ValidateTask validateTask;
	
	public UserDAO() {
		
	}
	
	public void isEmailAvailable(Context context, Request.Callback callback, String email) {
		if (Network.isOnline(context))
			startIsEmailAvailableTask(context, callback, email);
		else
			callback.onError(IsEmailAvailableTask.class, Request.Response.NoNetworkConnection, context.getString(R.string.exception_no_network_connection));
	}
	
	public boolean isLoginTaskRunning() {
		return loginTask != null && loginTask.getStatus() != AsyncTask.Status.FINISHED;
	}
	
	public boolean isOfflineTaskRunning() {
		return offlineTask != null && offlineTask.getStatus() != AsyncTask.Status.FINISHED;
	}
	
	public boolean isOnlineTaskRunning() {
		return onlineTask != null && onlineTask.getStatus() != AsyncTask.Status.FINISHED;
	}
	
	public boolean isRegisterTaskRunning() {
		return registerTask != null && registerTask.getStatus() != AsyncTask.Status.FINISHED;
	}
	
	public void isUsernameAvailable(Context context, Request.Callback callback, String username) {
		if (Network.isOnline(context))
			startIsUsernameAvailableTask(context, callback, username);
		else
			callback.onError(IsUsernameAvailableTask.class, Request.Response.NoNetworkConnection, context.getString(R.string.exception_no_network_connection));
	}
	
	public boolean isValidateTaskRunning() {
		return validateTask != null && validateTask.getStatus() != AsyncTask.Status.FINISHED;
	}
	
	public void login(Context context, Request.Callback callback, LoginListener listener, String username, String password) {
		if (Network.isOnline(context))
			startLoginTask(context, callback, listener, username, password);
		else
			callback.onError(LoginTask.class, Request.Response.NoNetworkConnection, context.getString(R.string.exception_no_network_connection));
	}
	
	public void online(Context context, Request.Callback callback, String id) {
		if (Network.isOnline(context))
			startOnlineTask(context, callback, id);
	}
	
	public void offline(Context context, Request.Callback callback, String id) {
		if (Network.isOnline(context))
			startOfflineTask(context, callback, id);
	}
	
	public void register(Context context, Request.Callback callback, RegisterListener listener, String id, String email,
			String username, String password, String description, String key) {
		if (Network.isOnline(context))
			startRegisterTask(context, callback, listener, id, email, username, password, description, key);
		else
			callback.onError(RegisterTask.class, Request.Response.NoNetworkConnection, context.getString(R.string.exception_no_network_connection));
	}
	
	public void validate(Context context, Request.Callback callback, String id, String key) {
		if (Network.isOnline(context))
			startValidateTask(context, callback, id, key);
		else
			callback.onError(ValidateTask.class, Request.Response.NoNetworkConnection, context.getString(R.string.exception_no_network_connection));
	}
	
	private void startIsEmailAvailableTask(Context context, Request.Callback callback, String email) {
		stopIsEmailAvailableTask();
		
		isEmailAvailableTask	= new IsEmailAvailableTask(context, callback);
		isEmailAvailableTask.execute(email);
	}
	
	private void startIsUsernameAvailableTask(Context context, Request.Callback callback, String username) {
		stopIsUsernameAvailableTask();
		
		isUsernameAvailableTask	= new IsUsernameAvailableTask(context, callback);
		isUsernameAvailableTask.execute(username);
	}
	
	private void startLoginTask(Context context, Request.Callback callback, LoginListener listener, String username, String password) {
		loginTask	= new LoginTask(context, callback, listener);
		loginTask.execute(username, password);
	}
	
	private void startOnlineTask(Context context, Request.Callback callback, String id) {
		onlineTask	= new OnlineTask(context, callback);
		onlineTask.execute(id);
	}
	
	private void startOfflineTask(Context context, Request.Callback callback, String id) {
		offlineTask	= new OfflineTask(context, callback);
		offlineTask.execute(id);
	}
	
	private void startRegisterTask(Context context, Request.Callback callback, RegisterListener listener, String id, String email,
			String username, String password, String description, String key) {
		registerTask	= new RegisterTask(context, callback, listener);
		registerTask.execute(id, email, username, password, description, key, String.valueOf(new Date().getTime()));
	}
	
	private void startValidateTask(Context context, Request.Callback callback, String id, String key) {
		validateTask	= new ValidateTask(context, callback);
		validateTask.execute(id, key);
	}
	
	private void stopIsEmailAvailableTask() {
		if (isEmailAvailableTask != null && isEmailAvailableTask.getStatus() != AsyncTask.Status.FINISHED)
			isEmailAvailableTask.cancel(true);
	}
	
	private void stopIsUsernameAvailableTask() {
		if (isUsernameAvailableTask != null && isUsernameAvailableTask.getStatus() != AsyncTask.Status.FINISHED)
			isUsernameAvailableTask.cancel(true);
	}

}
